package com.burgeon.backend.SHOPBACKEND.rest;

/**
 * 查询商品明细
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.ProductItem;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;
import java.util.*;

public class GetPRODUCTALIAS extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetPRODUCTALIAS(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */
    public HashMap getData(String id) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(id),url);

        System.err.println(str);
        JSONObject jsonObject = new  JSONArray(str).getJSONObject(0);
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<ProductItem> data = new ArrayList<ProductItem>();

        Set colorList = new HashSet();
        for (int i=0;i<jsonArray.length();i++){
            ProductItem productItem = new ProductItem();
            productItem.setId(jsonArray.getJSONArray(i).getString(0));
            productItem.setCode(jsonArray.getJSONArray(i).getString(1));
            productItem.setName(jsonArray.getJSONArray(i).getString(2));
            productItem.setASI(jsonArray.getJSONArray(i).getString(3));
            productItem.setColor(jsonArray.getJSONArray(i).getString(4));
            productItem.setSize(jsonArray.getJSONArray(i).getString(5));
           // productItem.setNum(jsonArray.getJSONArray(i).getString(6));
            productItem.setNum("0");
            data.add(productItem);
            colorList.add(productItem.getColor());
        }
        List<String> list_1 = new ArrayList<>(colorList);

        ArrayList data1 = new ArrayList();
        for (String s : list_1) {
            HashMap hashMap1 = new HashMap();
            ArrayList arrayList1 = new ArrayList();
            for (ProductItem datum : data) {
                if (s.equals(datum.getColor())) {
                    arrayList1.add(datum.getSize());
                }
            }
            hashMap1.put("color",s);
            hashMap1.put("size",arrayList1);
            data1.add(hashMap1);
        }
        ArrayList arrayList = new ArrayList();
        for (String s : list_1) {
            HashMap hashMap1 = new HashMap();
            ArrayList arrayList1 = new ArrayList();
            for (ProductItem datum : data) {
                if (s.equals(datum.getColor())) {
                    HashMap hashMap = new HashMap();
                    hashMap.put("key",datum.getSize());
                    hashMap.put("num",datum.getNum());
                    hashMap.put("code",datum.getCode());
                    hashMap.put("show",true);
                    arrayList1.add(hashMap);
                }
            }

            hashMap1.put("color",s);
            hashMap1.put("show",true);
            hashMap1.put("size",arrayList1);

            arrayList.add(hashMap1);
        }
        HashMap hashMap = new HashMap();
        hashMap.put("item",arrayList);

        HashMap row = new HashMap();
        row.put("data",data1);
        row.put("use",hashMap);
        return row;
    }

    private MultiValueMap Data(String id) {

        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'ID'");
        columns.add("'NO'");
        columns.add("'M_PRODUCT_ID;VALUE'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;NAME'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;NAME'");
      //  columns.add("QTYCAN");


        params.put("columns", columns);
        params.put("table", "M_PRODUCT_ALIAS");//表名

        params.put("count","true");
        HashMap orderby = new HashMap();

        orderby.put("column", "'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;MARTIXCOL'");

        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        String ac = "'="+id+"'";
        paramss.put("column", "'M_PRODUCT_ID'");
        paramss.put("condition", ac);
        params.put("params", paramss);
        /*HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("'M_PRODUCTALIAS_ID;ID'");
        columns.add("'M_PRODUCTALIAS_ID;NO'");
        columns.add("'M_PRODUCT_ID;VALUE'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;NAME'");
        columns.add("'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;NAME'");
        columns.add("QTYCAN");


        params.put("columns", columns);
        params.put("table", "FA_STORAGEYC_SKU");//表名

        params.put("count","true");
        HashMap orderby = new HashMap();

        orderby.put("column", "'M_PRODUCTALIAS_ID;M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;MARTIXCOL'");

        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        String ac = "'="+id+"'";
        paramss.put("column", "'M_PRODUCT_ID'");
        paramss.put("condition", ac);
        params.put("params", paramss);*/

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        GetPRODUCTALIAS getRetailByVip = new GetPRODUCTALIAS("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailByVip.getData("406587"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
